/*----------------------------------------------------------------------------*\
| /gamemode/cl_hud.lua                 | HUD system                      | CLI |
\*----------------------------------------------------------------------------*/

local hud = {}

// Initialize variables
local HUD_X = 0										// Origin X
local HUD_Y = 0										// Origin Y
local HUD_W = ScrW()								// Screen width
local HUD_H = ScrH()								// Screen height
local HUD_P = 2										// Padding
local HUD_B = 2										// Border
local HUD_BAR = 100									// Bar length

local HUD_ALPHA = 1.00								// Alpha
local HUD_VERSION = 0								// SVN Revision - AUTOMATICALLY UPDATED

local HUD_HEALTH = {}	// Health
HUD_HEALTH["red"]		= 0
HUD_HEALTH["green"]		= 0
HUD_HEALTH["blue"]		= 0
HUD_HEALTH["color"]		= Color(0,0,0,255)
HUD_HEALTH["color_b"]	= Color(0,0,0,255)

local HUD_ARMOR = {}	// Armor
HUD_ARMOR["red"]		= 0
HUD_ARMOR["green"]		= 0
HUD_ARMOR["blue"]		= 0
HUD_ARMOR["color"]		= Color(0,0,0,255)
HUD_ARMOR["color_b"]	= Color(0,0,0,255)
HUD_ARMOR["draw"] 		= false
HUD_ARMOR["length"] 	= 1
HUD_ARMOR["corner"] 	= 2
HUD_ARMOR["alpha"]		= 1
HUD_ARMOR["fadedir"]	= 0

local HUD_FATIGUE = {}	// Fatigue
HUD_FATIGUE["red"]		= 0
HUD_FATIGUE["green"]	= 0
HUD_FATIGUE["blue"]		= 0
HUD_FATIGUE["color"]	= Color(0,0,0,255)
HUD_FATIGUE["color_b"]	= Color(0,0,0,255)
HUD_FATIGUE["length"]	= 1
HUD_FATIGUE["draw"]		= false
HUD_FATIGUE["corner"]	= 2
HUD_FATIGUE["alpha"]	= 1
HUD_FATIGUE["fadedir"]	= 0

HUD_AMMO = -1
HUD_CLIPWIDTH = 12

local HUD_VALUES = {}
HUD_VALUES["health"] = 0
HUD_VALUES["armor"] = 1
HUD_VALUES["fatigue"] = ZDayConfig["MaxFatigue"]

local LOCALPLAYER_INITIALIZED		= false

function hud.Calc()

	HUD_VERSION = ZDayConfig["version"]

	local ply = LocalPlayer()

	// IF player is not alive, stop
	if (!ply:Alive() or ply:IsRagdolled()) then return end
	
	// Alpha
	local HUD_ALPHA = 0.97

	// Armor
	local ARM = ply:Armor() or 0
	HUD_ARMOR["red"]		= 255 - (ARM * 1.65)
	HUD_ARMOR["green"]		= 255 - (ARM * 1.40)
	HUD_ARMOR["blue"]		= 255 - (ARM * 0.90)
	HUD_ARMOR["color"]		= Color(HUD_FATIGUE["red"], HUD_FATIGUE["green"], HUD_FATIGUE["blue"], HUD_ALPHA * 200 * HUD_ARMOR["alpha"] )

	// Fatigue
	local FAT = ply:GetFatigue()
	local FATM = ZDayConfig["MaxFatigue"]
	HUD_FATIGUE["red"] 		= 255
	HUD_FATIGUE["green"]	= 255
	HUD_FATIGUE["blue"]		= 255 - (FAT * 0.20)
	HUD_FATIGUE["color"]	= Color(HUD_FATIGUE["red"], HUD_FATIGUE["green"], HUD_FATIGUE["blue"], HUD_ALPHA * 200 * HUD_FATIGUE["alpha"] )

	// Ammo
	-- Overwriting the SWEP table not a good idea. - Who made it overwrite the SWEP table -.-
	local wep	= ply:GetActiveWeapon()
	if (wep.ShowIndividualRounds) then
		wep.Primary.ClipSize=1
		HUD_CLIPWIDTH=8
	else
		HUD_CLIPWIDTH=16
	end

	if wep:IsValid() and wep.Primary != nil then
		local Clips = ( ply:GetAmmoCount( wep.Primary.Ammo ) or 0 ) / ( wep.Primary.ClipSize or 1 )
		HUD_AMMO = Clips
	end
			
end
timer.Create( "HUD Calc", 0.175, 0, function() hud.Calc() end )

function hud.FastCalc()
	if ( LOCALPLAYER_INITIALIZED ) then
		local ply = LocalPlayer()
		
		// IF player is not alive, stop
		if (!ply:Alive() or ply:IsRagdolled()) then return end
		
		// Health
		local HP = ply:Health()
		HUD_VALUES["health"]=math.Round(math.Approach(HUD_VALUES["health"], HP, 1 ))
		HUD_HEALTH["red"]		= 255 - (HUD_VALUES["health"] * 1.85)
		HUD_HEALTH["green"]		= HUD_VALUES["health"] * 1.08
		HUD_HEALTH["blue"]		= HUD_VALUES["health"] * 0.60 
		HUD_HEALTH["color"]		= Color(HUD_HEALTH["red"], HUD_HEALTH["green"], HUD_HEALTH["blue"], 200 * HUD_ALPHA)
		HUD_HEALTH["color_b"]	= Color(HUD_HEALTH["red"] * 0.75, HUD_HEALTH["green"] * 0.75, HUD_HEALTH["blue"] * 0.75, 255 * HUD_ALPHA)
		
		// Armor
		local ARM = ply:Armor()
		HUD_VALUES["armor"]=math.Round(math.Approach(HUD_VALUES["armor"], ARM, 1 ))
		HUD_ARMOR["length"]		= HUD_BAR * ( HUD_VALUES["armor"] / ZDayConfig["MaxArmor"] )
		HUD_ARMOR["draw"] 		= false
		if HUD_VALUES["armor"] > 4 and HUD_VALUES["armor"] then
			HUD_ARMOR["draw"] = true
		end
		HUD_ARMOR["corner"] = 2
		if HUD_VALUES["armor"] < 10 then HUD_ARMOR["corner"] = 0 end
		
		// Fatigue
		local FAT = ply:GetFatigue()
		local FATM = ZDayConfig["MaxFatigue"]
		HUD_VALUES["fatigue"]=math.Round(math.Approach(HUD_VALUES["fatigue"], FAT, 2 ))
		HUD_FATIGUE["length"]	= HUD_BAR * ( HUD_VALUES["fatigue"] / FATM )
		HUD_FATIGUE["draw"]		= false
		if HUD_VALUES["fatigue"] > 4 and HUD_VALUES["fatigue"] < FATM then
			HUD_FATIGUE["draw"] = true
		end
		HUD_FATIGUE["corner"] = 2
		if FAT < 10 then HUD_FATIGUE["corner"] = 0 end
	else
		LOCALPLAYER_INITIALIZED = true
	end
end
hook.Add("Think","HUD FastCalc",hud.FastCalc)


function hud.Draw()
	
	local ply = LocalPlayer()
	
	// Gotta draw the current revision
	if ( ZDayConfig["debug"] ) then draw.SimpleTextOutlined("Z-Day SVN Revision " .. ZDayConfig["version"], "ZDChat", 2, 2, Color(255,255,255,200*HUD_ALPHA), TEXT_ALIGN_LEFT, TEXT_ALIGN_TOP, 1, Color(0,0,0,200*HUD_ALPHA)) end
	
	// IF player is not alive, don't draw vitals and such
	if ( ply:Alive() and !ply:IsRagdolled() ) then
	
		// Scope
		if (ply:GetActiveWeapon().Mode == WEAPON_SCOPED and ply:GetActiveWeapon():GetNetworkedBool( "Ironsights" ) and MYFOV<59) then
			local ScW, ScH, ScopeId

			ScW = surface.ScreenWidth()
			ScH = surface.ScreenHeight()


			ScopeId = surface.GetTextureID("weapons/scopes/scope2")
			surface.SetTexture(ScopeId)

			QuadTable = {}

			QuadTable.texture 	= ScopeId
			QuadTable.color		= Color( 255, 255, 255, 255 )

			QuadTable.x = 0
			QuadTable.y = 0
			QuadTable.w = ScW
			QuadTable.h = ScH	
			draw.TexturedQuad( QuadTable )
		end

		// Health
		draw.RoundedBox(4, HUD_X + (3*HUD_P) + 16, HUD_Y + HUD_H - (3*HUD_P) - 80, 64, 64, Color(16,32,20,200*HUD_ALPHA) )
		draw.RoundedBox(2, HUD_X + (4*HUD_P) + 16, HUD_Y + HUD_H - (2*HUD_P) - 80, 60, 60, HUD_HEALTH["color"] )

		// Armor
		draw.RoundedBox(4, HUD_X+(2*HUD_P), HUD_Y+HUD_H-(3*HUD_P)-80,16,64,Color(16,32,20,200*HUD_ALPHA))
		draw.RoundedBox(HUD_ARMOR["corner"], HUD_X+(3*HUD_P), HUD_Y+HUD_H-(2*HUD_P)-80+(60-(HUD_ARMOR["length"]/10*6)),12,HUD_ARMOR["length"]/10*6,HUD_ARMOR["color"])

		
		// Fatigue
		draw.RoundedBox(4, HUD_X+(3*HUD_P)+16, HUD_Y + (HUD_H-(2*HUD_P)-16),64,16,Color(16,32,20,200*HUD_ALPHA))
		draw.RoundedBox(HUD_FATIGUE["corner"], HUD_X+(4*HUD_P) + 16, HUD_Y + HUD_H-HUD_P-16,HUD_FATIGUE["length"]/10*6, 12, HUD_ARMOR["color"] )
		
		// Infection notifier
		draw.RoundedBox(4, HUD_X+(2*HUD_P), HUD_Y + HUD_H - (2*HUD_P) - 16, 16, 16, Color(16,32,20,200*HUD_ALPHA))
		if (INFECTING) then
			draw.RoundedBox(4, HUD_X+(3*HUD_P), HUD_Y + HUD_H - HUD_P - 16, 16 - (2*HUD_P), 16 - (2*HUD_P), Color(255,0,0,200*HUD_ALPHA))
		end
		
		// Weapon switch
		if (wswitch.Switch) then
			surface.SetFont("ZDChat")
			local tw,th=surface.GetTextSize(wswitch.Names[wswitch.SelectWeapon])
			draw.SimpleTextOutlined(wswitch.Names[wswitch.SelectWeapon],"ZDChat",HUD_X+(HUD_W/2),HUD_Y+4,Color(255,255,255,200*HUD_ALPHA),TEXT_ALIGN_CENTER,TEXT_ALIGN_BOTTOM,1,Color(0,0,0,200*HUD_ALPHA))
		end
	
		// Ammunition
		if (HUD_AMMO>0) then
			draw.RoundedBox(4, HUD_X + HUD_W - (3*HUD_P) - 16 - (HUD_AMMO * HUD_CLIPWIDTH + HUD_P), HUD_Y + HUD_H - (2*HUD_P) - 16, HUD_AMMO * HUD_CLIPWIDTH + HUD_P, 16, Color(16,32,20,200*HUD_ALPHA))
			for i=1,HUD_AMMO do 
				draw.RoundedBox(2, HUD_X + HUD_W - (2*HUD_P) - 16 - ((HUD_AMMO - i + 1) * HUD_CLIPWIDTH + HUD_P) + 1, HUD_Y + HUD_H - HUD_P - 16, HUD_CLIPWIDTH - (2 * HUD_P), 16 - (2 * HUD_P), Color(128,148,132,200*HUD_ALPHA))
			end
			
		end
		
		draw.RoundedBox(4, HUD_X + HUD_W - (2*HUD_P) - 16, HUD_Y + HUD_H - (2*HUD_P) - 16, 16, 16, Color(16,32,20,200*HUD_ALPHA))

			
		// Time / goal notifier
		local goal = ""
		if (INGOALAREA) then goal = "[GOAL] "end
		draw.SimpleTextOutlined(goal..FormatTime(time.thetime),"ZDChat",HUD_W-HUD_P,HUD_P,Color(255,255,255,200*HUD_ALPHA),TEXT_ALIGN_RIGHT,TEXT_ALIGN_TOP,1,Color(0,0,0,200*HUD_ALPHA))
	
	end

end
hook.Add("HUDPaint", "HUD Draw", hud.Draw)

function HideThings( name )
	return 	name ~= "CHudHealth" 			and 
			name ~= "CHudBattery" 			and 
			name ~= "CHudAmmo" 				and 
			name ~= "CHudSecondaryAmmo" 	and 
			name ~= "CHudCrosshair" 		and 
			name ~= "CHudDamageIndicator" 	and 
			name ~= "CHudWeaponSelection"
end
hook.Add( "HUDShouldDraw", "HideThings", HideThings ) 

function GM:HUDWeaponPickedUp(wep)
end
function GM:HUDItemPickedUp(itemname)
end
function GM:HUDAmmoPickedUp(itemname,amount)
end
function GM:HUDDrawPickupHistory()
end
function GM:DrawDeathNotice(x,y)
end
function GM:HUDDrawTargetID()
	local tr = utilx.GetPlayerTrace( LocalPlayer(), LocalPlayer():GetCursorAimVector() )
	local trace = util.TraceLine( tr )
	if (!trace.Hit) then return end
	if (!trace.HitNonWorld) then return end
	local text = "ERROR"
	local font = "ZDTargetID"
	if (trace.Entity:IsPlayer()) then
		text = trace.Entity:Nick()
	elseif (trace.Entity:GetClass() == "npc_citizen") then
		//text = trace.Entity:GetName()
		text = "Citizen"
	else
		return
	end
	surface.SetFont( font )
	local w, h = surface.GetTextSize( text )

	local MouseX, MouseY = gui.MousePos()

	if ( MouseX == 0 && MouseY == 0 ) then

		MouseX = ScrW() / 2
		MouseY = ScrH() / 2

	end
	local x = MouseX
	local y = MouseY
	x = x - w / 2
	y = y + 30
	// The fonts internal drop shadow looks lousy with AA on
	draw.SimpleText( text, font, x+1, y+1, Color(0,0,0,120) )
	draw.SimpleText( text, font, x+2, y+2, Color(0,0,0,50) )
	draw.SimpleText( text, font, x, y, self:GetTeamColor( trace.Entity ) )
	y = y + h + 5
	//local text = trace.Entity:Health()
	local text = ""
	local traceHealth = trace.Entity:Health()
	//if (traceHealth == 0) then traceHealth = trace.Entity:GetKeyValues().health end
	if ( traceHealth < 100 ) then text = "Slightly injured" end
	if ( traceHealth < 75 ) then text = "Injured" end
	if ( traceHealth < 50 ) then text = "Gravely injured" end
	if ( traceHealth < 25 ) then text = "Almost dead" end
	
	text = text .. " (" .. traceHealth .. ")"
	
	local healthcolor = Color(255,255,255,255)
	if (zday.infection.IsInfected(trace.Entity)) then healthcolor = Color(128,72,48) end
	local font = "TargetIDSmall"
	surface.SetFont( font )
	local w, h = surface.GetTextSize( text )
	local x =  MouseX  - w / 2
	draw.SimpleText( text, font, x+1, y+1, Color(0,0,0,120) )
	draw.SimpleText( text, font, x+2, y+2, Color(0,0,0,50) )
	draw.SimpleText( text, font, x, y, healthcolor )
end



local WorldTip = nil

local TipColor = Color( 250, 250, 200, 255 )

function AddWorldTip( unused1, text, unused2, pos, ent )

	WorldTip = {}
	
	WorldTip.dietime 	= SysTime() + 0.05
	WorldTip.text 		= text
	WorldTip.pos 		= pos
	WorldTip.ent 		= ent
	
end


local function DrawWorldTip( tip )

	if ( tip.ent && tip.ent != NULL ) then	
		tip.pos = tip.ent:GetPos()
	end
	
	local pos = tip.pos:ToScreen()
	
	local black = Color( 0, 0, 0, 255 )
	local tipcol = Color( TipColor.r, TipColor.g, TipColor.b, 255 )
	
	local x = 0
	local y = 0
	local padding = 10
	local offset = 50
	
	surface.SetFont( "ZDTip" )
	local w, h = surface.GetTextSize( tip.text )
	
	x = pos.x - w 
	y = pos.y - h 
	
	x = x - offset
	y = y - offset

	draw.RoundedBox( 8, x-padding-2, y-padding-2, w+padding*2+4, h+padding*2+4, black )
	
	
	local verts = {}
	verts[1] = { x=x+w/1.5-2, y=y+h+2 }
	verts[2] = { x=x+w+2, y=y+h/2-1 }
	verts[3] = { x=pos.x-offset/2+2, y=pos.y-offset/2+2 }
	
	draw.NoTexture()
	surface.SetDrawColor( 0, 0, 0, tipcol.a )
	surface.DrawPoly( verts )
	
	
	draw.RoundedBox( 8, x-padding, y-padding, w+padding*2, h+padding*2, tipcol )
	
	local verts = {}
	verts[1] = { x=x+w/1.5, y=y+h }
	verts[2] = { x=x+w, y=y+h/2 }
	verts[3] = { x=pos.x-offset/2, y=pos.y-offset/2 }
	
	draw.NoTexture()
	surface.SetDrawColor( tipcol.r, tipcol.g, tipcol.b, tipcol.a )
	surface.DrawPoly( verts )
	
	
	draw.DrawText( tip.text, "ZDTip", x + w/2, y, black, TEXT_ALIGN_CENTER )

end


function GM:PaintWorldTips()

	if ( WorldTip && WorldTip.dietime > SysTime() ) then
		DrawWorldTip( WorldTip )		
	end

end
